<template>
  <div class="search-result">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="`${$route.query.keyword}的搜索结果`"
      left-arrow
      fixed
      @click-left="
        $router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      v-model="loading"
      class="article-list"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id"
        :title="item.title"
        @click="articleDesc(item.art_id)"
      />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { reqSearchResult } from '@/api/search'
export default {
  name: 'SearchResult',
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },

  methods: {
    async  onLoad() {
      const keyWord = this.$route.query.keyword
      const { data: { results }} = await reqSearchResult(keyWord, this.page)
      this.page++
      console.log(results)
      this.list.push(...results)
      this.loading = false
      if (results.length < 10) this.finished = true
    },
    articleDesc(n) {
      this.$router.push('/articleDesc/' + n)
    }
  }
}
</script>

<style lang="scss" scoped>
.search-result {
  .article-list {
    margin-top: 39px;
  }
}
</style>
